 /*
 * @description: 商品数据分析
 * @Author: along
 * @Date: 2019-09-03
 * @Last Modified by: along
 * @Last Modified time: 2020-09-03
 */
<template>
    <div class="container shopCommodity">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">商品时间:</p>
                    <el-date-picker
                        v-model="date"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">分类:</p>
                    <el-cascader
                        placeholder="请选择"
                        :options="dataList"
                        filterable
                        clearable
                        change-on-select
                        expand-trigger="click"
                        v-model="formData.type"
                        @change="changeType(1)"
                        size="small"
                        :props="{
                            label: 'title',
                            value: 'no',
                            children: 'children'
                        }"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">商品标题:</p>
                    <el-input
                        v-model="formData.keyword"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getShopName(1)"
                        @keyup.native.enter="getShopName(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">商品编号:</p>
                    <el-input
                        v-model="formData.goods_no"
                        placeholder="请输入商品编号"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getShopGoods(1)"
                        @keyup.native.enter="getShopGoods(1)"
                    />
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="shopCommodity"
            @sort-change="handleSortChanged"
            v-loading="loading"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
                fixed="left"
            />
            <el-table-column
                prop="goods_title"
                label="商品标题"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="type_name"
                label="分类"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="view_count"
                label="商品PV"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="100"
            />
            <el-table-column
                prop="view_user_count"
                label="商品UV"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="100"
            />
            <el-table-column
                prop="view_count_new_user"
                label="新用户商品PV"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="150"
            />
            <el-table-column
                prop="view_user_count_new_user"
                label="新用户商品UV"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="150"
            />
            <el-table-column
                prop="buy_user_count"
                label="下单人数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="110"
            />
            <el-table-column
                prop="buy_user_count_new_user"
                label="下单新用户人数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="150"
            />
            <el-table-column
                prop="pay_order_count"
                label="订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                align="center"
                :width="110"
            />
            <el-table-column
                prop="pay_money"
                label="下单金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="110"
            />
            <el-table-column
                prop="pay_money_new_user"
                label="下单新用户金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="150"
            />
        </el-table>
        <!-- /table -->

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
import {getDate} from '@/assets/js/utils';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            dataList: [],
            formData: {
                type: '',
                keyword: '',
                goods_no: ''
            },
            date: [
                this.getDate(0),
                this.getDate(1)
            ],
            tableData: [],
            copyTableData: [],
            authResult: {},
            loading: false
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '数据分析'},{title: '商品数据分析'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getTypeList();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData () {
            this.loading = true;
            this.$post('/student/statistic_shop%5Cget_single_goods_statistic', {
                start_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[0]))),
                end_time: this.date === null ? (new Date().getTime() / 1000) : (this.date.length == 0 ? (new Date().getTime() / 1000) : getDate(new Date(this.date[1])))
            }, resp => {
                if (resp.code == 1) {
                    let list = JSON.parse(JSON.stringify(resp.data.list));
                    let showList = resp.data.list.slice(0,50);
                    let lastList = resp.data.list.slice(50, resp.data.list.length);

                    this.tableData = showList;
                    // this.tableData = resp.data.list;

                    setTimeout(()=>{
                        this.tableData = this.tableData.concat(lastList);
                    },300);


                    this.copyTableData = JSON.parse(JSON.stringify(resp.data.list));
                    this.loading = false;
                }
            });
        },

        /**
         * @description 获取分类编号
         */
        getTypeList () {
            this.$post('/student/shop_goods_type%5Cget_list_linkage', {}, resp => {
                if (resp.code == 1) {
                    this.dataList = resp.data.list;
                }
            });
        },

        /**
         * @description 类别筛选
         */
        changeType () {
            if(this.formData.type.length == 1) {
                const parent_type_no = this.formData.type.length > 0 ? this.formData.type[0] : '';

                // console.log('parent_type_no', parent_type_no);

                if(parent_type_no) {
                    let list = [];

                    this.copyTableData.filter(item=> {

                        if(item.parent_type_no == parent_type_no) {
                            list.push(item);
                        }
                    });

                    this.tableData = JSON.parse(JSON.stringify(list));
                } else {
                    this.tableData = JSON.parse(JSON.stringify(this.copyTableData));
                    return false;
                }
            } else if (this.formData.type.length === 2) {
                const type_no = this.formData.type.length > 0 ? this.formData.type[this.formData.type.length - 1] : '';

                // console.log('type_no', type_no);

                if(type_no) {
                    let list = [];

                    this.copyTableData.filter(item=> {
                        if(item.type_no == type_no) {
                            list.push(item);
                        }
                    });

                    this.tableData = JSON.parse(JSON.stringify(list));
                } else {
                    this.tableData = JSON.parse(JSON.stringify(this.copyTableData));
                    return false;
                }

            } else {
                this.tableData = JSON.parse(JSON.stringify(this.copyTableData));
                return false;
            }
        },

        /**
         * @description 商品标题筛选
         */
        getShopName () {
            const goods_title = this.formData.keyword;

            if(goods_title) {
                let list = [];

                this.copyTableData.filter(item=> {
                    if(item.goods_title.indexOf(goods_title) > -1) {
                        list.push(item);
                    }
                });

                this.tableData = JSON.parse(JSON.stringify(list));
            } else {
                this.tableData = JSON.parse(JSON.stringify(this.copyTableData));
                return false;
            }
        },

        /**
         * @description 商品编号筛选
         */
        getShopGoods () {
            const goods_no = this.formData.goods_no;

            if(goods_no) {
                let list = [];

                this.copyTableData.filter(item=> {
                    if(item.goods_no.indexOf(goods_no) > -1) {
                        list.push(item);
                    }
                });

                this.tableData = JSON.parse(JSON.stringify(list));
            } else {
                this.tableData = JSON.parse(JSON.stringify(this.copyTableData));
                return false;
            }
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            this.sortTable(data.order, data.prop);
        },

        /**
         * @description 排序
         *      descending
         *      ascending
         */
        sortTable (type, param) {
            if(type == 'descending') {
                this.tableData = this.tableData.sort((a,b) => {
                    return a[param] - b[param];
                });
            }

            if(type == 'ascending') {
                this.tableData = this.tableData.sort((a,b) => {
                    return b[param] - a[param];
                });
            }
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        getDate (day) {
            let tempDate = new Date();
            let retult_milliseconds = tempDate.getTime() + 1000 * 60 * 60 * 24 * day;

            tempDate.setTime(retult_milliseconds);

            let result_Year = tempDate.getFullYear(),
                result_Month = (tempDate.getMonth() + 1).toString(),
                result_Date = tempDate.getDate().toString();

            if (result_Month.length === 1) {
                result_Month = "0" + result_Month;
            }

            if (result_Date.length === 1) {
                result_Date = "0" + result_Date;
            }

            let date = result_Year + "-" + result_Month + "-" + result_Date + " " + "00:00:00";

            return new Date(date).getTime();
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .shopCommodity{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
    }
</style>
